﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
//using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TimeTracker
{

    public partial class uTracker : UserControl
    {
        //public delegate void TrackerEventHandler(object sender, EventArgs e);
        /// <summary> Включение tracker-а
        /// </summary>
        public event EventHandler EventTrackerStart = null;
        
        /// <summary> Включение tracker-а
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnTrackerStart(EventArgs e)
        {
            if (EventTrackerStart != null)
            {
                EventTrackerStart(this, e);
            }
        }

                
        /// <summary> Показания счетчика времени
        /// </summary>
        public TimeSpan TCounter
        {
            get
            {
                return tCounter;
            }
            set
            {
                tCounter = value;
                lblTime.Text = tCounter.ToString();
            }
        }
        TimeSpan tCounter = new TimeSpan(0, 0, 0);

        /// <summary> Состояние трекера
        /// </summary>
        public Boolean TrackerOn
        {
            get
            {
                return trackerOn;
            }
        }
        Boolean trackerOn = false;

        public string TaskText
        {
            get
            {
                return txtTaskName.Text;
            }
            set
            {
                txtTaskName.Text = value;
            }
        }

        public Boolean TextAccess
        {
            get
            {
                return txtTaskName.Enabled;
            }
            set
            {
                txtTaskName.Enabled = value;
            }
        }

        TimeSpan ts = new TimeSpan(0, 0, 1);    // Дискрета приращения счетчика.
        
        public uTracker()
        {
            InitializeComponent();
            lblTime.Text = tCounter.ToString();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            tCounter = tCounter.Add(ts);
            lblTime.Text = tCounter.ToString();
        }

        private void btnCtrl_Click(object sender, EventArgs e)
        {
            ChngTrackerState();
        }

        public void ChngTrackerState()
        {
            if (trackerOn)
            {
                TrackerStop();
            }
            else
            {
                TrackerStart();
            }
        }

        public void TrackerStart()
        {
            if (txtTaskName.Text.Trim() == String.Empty)
            {
                MessageBox.Show("Для включения таймера укажите его название.");
                return;
            }

            OnTrackerStart(new EventArgs());
            trackerOn = true;
            btnCtrl.ImageKey = "Stop";
            timer.Enabled = true;
            btnClear.Enabled = false;
        }

        public void TrackerStop()
        {
            trackerOn = false;
            btnCtrl.ImageKey = "Start";
            timer.Enabled = false;
            btnClear.Enabled = true;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            tCounter = TimeSpan.FromSeconds(0);
            lblTime.Text = tCounter.ToString();
        }
    }
}
